package org.stvd.controller.quartz;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.stvd.common.aspectj.lang.annotation.Log;
import org.stvd.common.aspectj.lang.enums.BusinessType;
import org.stvd.common.aspectj.lang.enums.OperatorType;
import org.stvd.common.utils.InterfaceResult;
import org.stvd.service.quartz.impl.QuartzHandler;

/**
 * @title QuartzSchedulerController
 * @description 定时器控制类
 * @auther houzx
 * @date 2022年03月13日
 */
@RestController
@RequestMapping("quartz/scheduler")
public class QuartzSchedulerController {

    @Autowired
    private QuartzHandler quartzHandler;

    /**
     * 启动定时器
     * @return 启动结果
     */
    @PostMapping(value = "start")
    @Log(title = "启动定时器", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public InterfaceResult<Map<String, Object>> startScheduler() {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        boolean isStarted = quartzHandler.startScheduler();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("isStarted", isStarted);
        result.setResult(resultMap);
        result.setMsg("定时器已启动");
        return result;
    }

    /**
     * 暂停定时器
     * @return 暂停结果
     */
    @PostMapping(value = "stand")
    @Log(title = "暂停定时器", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public InterfaceResult<Map<String, Object>> standbyScheduler() {
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        boolean isStanded = quartzHandler.standbyScheduler();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("isStanded", isStanded);
        result.setResult(resultMap);
        result.setMsg("定时器已暂停");
        return result;
    }
}
